
function TracingListener() {
	this.originalListener = null;
	this.offset = 0;
	this.filecontent= null;
}
TracingListener.prototype =
{
	replacement:function (request, context, inputStream, offset, count,filename){
		ulttool.loginfo('TracingListener: request.name is:'+request.name);
		ulttool.loginfo('this.reqlength:'+this.reqlength);
//	    ulttool.loginfo('this.offset is:'+this.offset);
	    ulttool.loginfo('getdata from:'+offset+'<-->to:'+(offset+count));
	    
	    if(this.filecontent != null){
			filecontent = ulttool.loadbytefile(filename);
		}else{
			filecontent = this.filecontent;
		}
		
		var binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1","nsIBinaryInputStream");
		var storageStream = CCIN("@mozilla.org/storagestream;1", "nsIStorageStream");
    	var binaryOutputStream = CCIN("@mozilla.org/binaryoutputstream;1","nsIBinaryOutputStream");
            
        binaryInputStream.setInputStream(inputStream);
//        ulttool.loginfo(ulttool.max(filecontent.length,this.reqlength));
    	storageStream.init(8192,filecontent.length, null);
    	binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));
    	// read org-stream but never use.
    	var data = binaryInputStream.readBytes(count);
		binaryOutputStream.writeBytes(filecontent, filecontent.length);
		
		if(count+offset<this.reqlength){
//			ulttool.loginfo('senddata from:'+this.offset+'<-->to:'+(this.offset+1000));
			this.count = (count+offset)/this.reqlength * filecontent.length;
			this.count = parseInt(this.count.toString());
			if (this.count == filecontent.length){
				this.count = this.count-this.offset-1;
			}else{
				this.count = this.count-this.offset;
			}
			ulttool.loginfo('senddata from:'+this.offset+'<-->to:'+(this.offset+this.count));
			this.originalListener.onDataAvailable(request, context,storageStream.newInputStream(this.offset),this.offset,this.count);
			this.offset+=this.count;
		}
//		else{
//			var filllen = 0;
//			if (filecontent.length > this.reqlength){
//				this.count = filecontent.length - this.offset;
//			}else{
//				this.count = this.reqlength - this.offset;
//				filllen = this.reqlength - filecontent.length;
//			}
//			while(filllen>0){
//				filllen--;
//				binaryOutputStream.writeBytes(0x00,1);
//			}
			this.count = filecontent.length - this.offset;
			ulttool.loginfo('senddata from:'+this.offset+'<-->to:'+(this.offset+this.count));
			this.originalListener.onDataAvailable(request, context,storageStream.newInputStream(this.offset),this.offset,this.count);
		}
	},
    onDataAvailable: function(request, context, inputStream, offset, count) {

//    	if (request.name=='http://v.youku.com/crossdomain.xml'){
//	    	this.replacement(request, context, inputStream, offset, count,'crossdomain.xml')
//    		return;
//	    }
	    
	    if(request.name.search(/http:\/\/static\.youku\.com(\/v[\d\.]*)?\/v\/swf\/player.*\.swf/) >= 0){
	    	this.replacement(request, context, inputStream, offset, count,'player.swf')
    		return;
	    }
	    
	    if(request.name.search(/http:\/\/player\.ku6cdn\.com\/default\/common\/player\/\d*\/player\.swf/) >= 0){
	    	this.replacement(request, context, inputStream, offset, count,'ku6.swf')
    		return;
	    }

    	try{
    		this.originalListener.onDataAvailable(request, context, inputStream, offset, count);
    	}catch(err){
    	}
    },

    onStartRequest: function(request, context) {
//    	request.QueryInterface(Ci.nsIHttpChannel);   	
        this.originalListener.onStartRequest(request, context);
    },

    onStopRequest: function(request, context, statusCode) {
//    	ulttool.loginfo('onStopRequest: context is:'+context);
//		httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
//		reqURL = request.URI.spec;
////		ulttool.loginfo('onStopRequest reqURL:'+reqURL);
//		if(reqURL=='http://v.youku.com/crossdomain.xml'){
////			var filename = 'crossdomain.xml';
////	    	filecontent = ulttool.loadfile(filename,null);
////    	  	request.setRequestHeader('Content-Length',''+filecontent.length,true);
//    	  	ulttool.loginfo('Content-Length:'+httpChannel.getResponseHeader('Content-Length'));
//    	}
//    	if(reqURL.search(/http:\/\/static\.youku\.com(\/v[\d\.]*)?\/v\/swf\/player.*\.swf/) >= 0){
//    		var filename = 'player.swf';
//	    	filecontent = ulttool.loadbytefile(filename);
//	    	ulttool.loginfo('Content-Length:'+httpChannel.getResponseHeader('Content-Length'));
////	    	httpChannel.setResponseHeader("Content-Length",filecontent.length,true);
//    	}
        this.originalListener.onStopRequest(request, context, statusCode);
    },

    QueryInterface: function (aIID) {
        if (aIID.equals(Ci.nsIStreamListener) ||
            aIID.equals(Ci.nsISupports)) {
            return this;
        }
        throw Components.results.NS_NOINTERFACE;
    }
}